package com.atwisdom.star.core.bsl.erp;

import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.vo.ResultInfo;
import com.atwisdom.star.common.vo.StatusCode;
import com.atwisdom.star.exception.pojo.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @Date: 2023-12-25 10:52
 */
@Slf4j
@Component
public class ErpServiceClient {

    @Autowired
    private RestTemplate restTemplate;

    private String erpUrl = "http://localhost:10000/tz";

    /**
     * 物料新增
     * @param dataJson
     * @return
     */
    public ErpResponse materialAdd(JSONObject dataJson) {
        String url =  ErpConstant.BASE_URL +  ErpConstant.MATERIAL_ADD;
        List<JSONObject> childrenvo = new ArrayList<>();
        // 获取编号与名称
        JSONObject values = dataJson.getJSONObject("values");
        String code = values.getString("value");
        String name = values.getString("label");

        JSONObject childParamJson = new JSONObject();
        childParamJson.put("mainmeasrate", values.get("conversionRate"));
        childParamJson.put("pk_measdoc", values.get("unit"));
        childrenvo.add(childParamJson);


        JSONObject parentVo = new JSONObject();
        parentVo.put("invcode", values.get("bcode"));
        parentVo.put("invname", values.get("name"));
        parentVo.put("pk_invcl", values.get("name"));
        JSONObject invbasdoc = new JSONObject();
        invbasdoc.put("childrenvo", childrenvo);
        invbasdoc.put("parentvo", parentVo);

        JSONObject bodyJson = new JSONObject();
        bodyJson.put("invbasdoc", invbasdoc);

        return postForObject(url, bodyJson);
    }

    /**
     * 物料分类删除
     * @param code  物料分类编码
     * @return
     */
    public ErpResponse bdinvclDelete(String code) {
        String url =  ErpConstant.BASE_URL +  ErpConstant.BDINVCL_DELETE;
        JSONObject paramJson = new JSONObject();
        paramJson.put("code", code);
        return postForObject(url, paramJson);
    }

    /**
     * 物料分类新增
     * @param dataJson
     * @return
     */
    public ErpResponse bdinvclSave(JSONObject dataJson) {
        String url =  ErpConstant.BASE_URL +  ErpConstant.BDINVCL_SAVE;
        List<JSONObject> invclList = new ArrayList<>();
        // 获取编号与名称
        JSONObject values = dataJson.getJSONObject("values");
        String code = values.getString("value");
        String name = values.getString("label");

        JSONObject paramJson = new JSONObject();
        // 缺少分类编码
        paramJson.put("invclasscode", "code");
        paramJson.put("invclassname", "name");
        invclList.add(paramJson);
        JSONObject bodyJson = new JSONObject();
        bodyJson.put("invcl", invclList);
        return postForObject(url, bodyJson);
    }

    /**
     * 物料分类新增
     * invcl		是	是
     * pk_invcl	ERP存货分类主键
     * @param dataJson
     * @return
     */
    public ErpResponse bdinvclUpdate(JSONObject dataJson) {
        String url =  ErpConstant.BASE_URL +  ErpConstant.BDINVCL_UPDATE;
        List<JSONObject> invclList = new ArrayList<>();
        // 获取编号与名称
        JSONObject values = dataJson.getJSONObject("values");
        String code = values.getString("value");
        String name = values.getString("label");
        String pkCorp = values.getString("pk_corp");

        // Long id = values.getLong("id");

        JSONObject paramJson = new JSONObject();
        paramJson.put("invclasscode", "code");
        paramJson.put("invclassname", "name");
        paramJson.put("pk_corp", pkCorp);
        invclList.add(paramJson);
        JSONObject bodyJson = new JSONObject();
        bodyJson.put("invcl", invclList);
        return postForObject(url, bodyJson);
    }


    /**
     * 物料分类新增
     * @param url
     * @param data
     * @return
     */
    public ErpResponse postForObject(String url, JSONObject data) {
        HttpEntity<String> request = buildRequest(data);
        ErpResponse erpResponse = restTemplate.postForObject(url, request, ErpResponse.class);
        return handleResponse(erpResponse);
    }

    /**
     * 处理返回信息
     * @param erpResponse
     * @return
     */
    private ErpResponse handleResponse(ErpResponse erpResponse) {
        String responseStatus = erpResponse.getStatus();
        log.info("erpResponse status = {}", responseStatus);
        if ("success".equals(responseStatus)) {

        } else {
            // 说明是failed
            String errorMsg= erpResponse.getErrormsg();
            String errorCode = erpResponse.getErrorcode();
            throwException(errorCode + errorMsg);
        }
        return erpResponse;
    }

    /**
     * 构建请求数据
     * @return
     */
    private HttpEntity<String> buildRequest(JSONObject body) {
        HttpHeaders headers = buildHeaders();
        // 请求体
        HttpEntity<String> request = new HttpEntity(body.toString(), headers);
        return request;
    }

    /**
     * 构建请求头
     * @return
     */
    private HttpHeaders buildHeaders() {
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("usercode", "usercode");
        headers.add("password", "password");
        headers.add("system", "111111");
        return headers;
    }

    private void throwException(String msg) {
        throw new BusinessException(StatusCode.ERROR_ERP_ERROR.getCode(), msg);
    }
}
